#ifndef SHAREDATA_H
#define SHAREDATA_H

#include "serialcontroller.h"
#include "qserialproto.h"
#include "network_controller.h"
#include <QObject>
#include <QDebug>
#include "lua_wraper.h"
#include <QtQuickWidgets/QQuickWidget>
#include "tcp_server_libevent.h"

class QmlShareData;

extern QmlShareData gGlobal;

// 桥接Lua虚拟机和qml界面
class QmlShareData : public QObject ,SerialController::SerialListener
{
    Q_OBJECT
    Q_PROPERTY(QString _txt READ rtxt WRITE settxt NOTIFY txtchanged)
public:
    explicit QmlShareData(QObject *parent = nullptr);
    int SetQuickView(QQuickWidget *);
    QString rtxt() const{
        return _txt;
    }
    void settxt(QString s){
        _txt = s;
        emit txtchanged();
    }
    int OnDataRecv(QByteArray);
    int ShowDataInQML(QString x);
    int SendUartData(const char *);
    Q_INVOKABLE void getValFromQml(int v) {
        emit valueFromCpp(456);
    }
    Q_INVOKABLE int openUart(QString port,QString baudRate,QString dataBits,QString stopBits,
                             QString flow);
    Q_INVOKABLE int openNetwork(QString ip,unsigned int port,bool is_ws,int type);
    Q_INVOKABLE int closeNetwork(QString);
    Q_INVOKABLE int closeSerial();
    Q_INVOKABLE int TestLua();
    Q_INVOKABLE int TestShowData();
    Q_INVOKABLE int updateLuaScript(QString);
    Q_INVOKABLE int saveLuaScript(QString);
    Q_INVOKABLE bool luaStatus();
    Q_INVOKABLE bool DoLuaCmd(QString);
    QString _txt = "hello world\r\n";
signals:
    void txtchanged();
    void valueFromCpp(int val);
    void sendToQml(int count);
    void disConnected();
    void clearBuffer();

public slots:
    void on_network_data_recv();
    void on_network_conected(string,uint);
    void on_network_disconected(string,uint);
    void on_udp_data_recv();
    void on_network_server_read(string);

private:
    TcpServerLibevent *m_tcp_server;
    NetworkController *m_network_;
    SerialController *m_serial_controller_;
    LuaDelegate m_lua_;
    QObject *m_qml_protodebug_;
    QObject *m_root_obj_;
    bool m_luavm_status_;
    QString m_lua_string;
    QQuickWidget *m_qml_view_;
};


#endif // SHAREDATA_H
